/*   Copyright 2012 Julian Aloofi

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include "ScreenManager.hpp"

Tacticus::ScreenManager *Tacticus::ScreenManager::instance = 0;

Tacticus::ScreenManager &Tacticus::ScreenManager::getInstance()
{
    if (!instance)
        instance = new ScreenManager();
    return *instance;
}

void Tacticus::ScreenManager::destroy()
{
    if (instance)
        delete instance;
    instance = 0;
}

const Tacticus::Screen &Tacticus::ScreenManager::getCurrentScreen() const
{
    return *currentScreens.back();
}

void Tacticus::ScreenManager::processCurrentInput()
{
    currentScreens.back()->processInput();
}

void Tacticus::ScreenManager::drawCurrentScreen()
{
    const std::list<boost::shared_ptr<ScreenElement> > &elementList =
        currentScreens.back()->getScreenElements();
    WindowManager::getInstance().getMainWindow().clear();
    for (std::list<boost::shared_ptr<ScreenElement> >::const_iterator it =
        elementList.begin(); it != elementList.end(); it++)
    {
        if (!(*it))
            continue;
        WindowManager::getInstance().getMainWindow().draw(
            *(*it)->getLayeredDrawable().second);
    }
    WindowManager::getInstance().getMainWindow().display();
}

void Tacticus::ScreenManager::handleEvent(boost::shared_ptr<Tacticus::Event>
    event)
{
    switch (event->getType())
    {
    case Events::PROCESSINPUT_EVENT:
    {
        processCurrentInput();
        break;
    }
    case Events::DRAWSCREEN_EVENT:
    {
        drawCurrentScreen();
        break;
    }
    case Events::NEWGAME_EVENT:
    {
        currentScreens.clear();
        currentScreens.push_back(boost::shared_ptr<MainMenuScreen>(
            new MainMenuScreen()));
        break;
    }
    case Events::STARTHOTSEAT_EVENT:
    {
        currentScreens.clear();
        currentScreens.push_back(boost::shared_ptr<GameScreen>(
            new GameScreen()));
        currentScreens.back()->handleEvent(event);
        break;
    }
    case Events::SUCCESSFULPLACEMENT_EVENT:
    {
        currentScreens.back()->handleEvent(event);
        break;
    }
    case Events::SUCCESSFULPLACEMENTDONE_EVENT:
    {
        currentScreens.back()->handleEvent(event);
        break;
    }
    case Events::SUCCESSFULTURN_EVENT:
    {
        currentScreens.back()->handleEvent(event);
        break;
    }
    case Events::SHOWHOTSEATSWITCHDIALOG_EVENT:
    {
        currentScreens.push_back(boost::shared_ptr<ConfirmOKDialogScreen>(
            new ConfirmOKDialogScreen("Change seats")));
        break;
    }
    case Events::SHOWDRAWTURNSCREEN_EVENT:
    {
        boost::shared_ptr<ShowDrawTurnScreenEvent> drawTurnScreenEvent =
            boost::static_pointer_cast<ShowDrawTurnScreenEvent>(event);
        currentScreens.push_back(boost::shared_ptr<DrawTurnScreen>(
            new DrawTurnScreen(drawTurnScreenEvent->getOrigin(),
            drawTurnScreenEvent->getTarget(),
            drawTurnScreenEvent->playersInversed())));
        break;
    }
    case Events::CONFIRMOKDIALOG_EVENT:
    {
        currentScreens.pop_back();
        break;
    }
    case Events::GAMEOVER_EVENT:
    {
        boost::shared_ptr<GameOverEvent> gameOverEvent =
            boost::static_pointer_cast<GameOverEvent>(event);
        currentScreens.clear();
        currentScreens.push_back(boost::shared_ptr<MainMenuScreen>(
            new MainMenuScreen()));
        currentScreens.push_back(boost::shared_ptr<ConfirmOKDialogScreen>(
            new ConfirmOKDialogScreen("Game over. " +
            gameOverEvent->getWinner().str() + " won.",
            boost::shared_ptr<NewGameEvent>(new NewGameEvent()))));
        break;
    }
    default:
    {
        throw Common::EventHandlingException(
            "Unhandled event passed to ScreenManager");
    }
    }
}
